/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.tigris.jasmincc.implementation;

import java.util.*;

import org.tigris.jasmincc.Production;
import org.tigris.jasmincc.framework.JavaCCModel;
import org.tigris.jasmincc.framework.NamingManager;
import org.tigris.mappedlexer.validator.*;

/**
 * Pases method declaration unit.
 * 
 * @author Sam Huang
 * 
 */
public class MethodDeclarationProduction extends LexerSwitchingProduction {

	protected Map<String, Object> cache = null;

	protected JavaCCModel javaccModel = null;

	protected NamingManager namingManager = null;

	protected Production methodBodyProduction = null;

	protected Map<String, String> params = new HashMap<String, String>();

	@Override
	public void onParse() throws Exception {
		org.tigris.mappedlexer.io.CharOutputSequence outputSequence = javaccModel
				.addMethod();
		lexer.setCharOutputSequence(outputSequence);

		StringBuffer methodHead = new StringBuffer();

		String returnType = lexer.nexttok(false, VerifyHub.IDENTIFIER);
		methodHead.append(returnType);

		tk = lexer.nexttok(false);
		if (tk.equals(SYM_LEFT_SQURE)) {
			// lexer.print("[");
			methodHead.append(tk);
			tk = lexer.nexttok(false, SYM_RIGHT_SQURE);
			methodHead.append(tk);
			tk = lexer.nexttok(false, VerifyHub.IDENTIFIER);
		}

		/**
		 * FEATURE: void main(String[] args) in JavaCC grammar to write a main
		 * method
		 */
		if (tk.equals(JAVA_METHOD_MAIN)) {
			methodHead = new StringBuffer("public static "
					+ methodHead.toString());
		}
		namingManager = (NamingManager) cache.get("namingManager");
		String qualifiedMethodName = namingManager.prefixWithInstanceName(tk);

		// if the method already appeared, surely we ignore this one.
		if (namingManager.containsDeclaration(qualifiedMethodName)) {
			lexer.setDump(false);
		} else {
			namingManager.addDeclaration(qualifiedMethodName);
			lexer.print(methodHead.toString());
		}
		lexer.print(" ").print(qualifiedMethodName);

		// lexer.print(returnType).print(" ").print(qualifiedMethodName);

		tk = lexer.nexttok(false, SYM_LEFT_ROUND_BRACE);
		lexer.skip(true, SYM_LEFT_ROUND_BRACE_CHAR);
		// parseMethodParameters();
		/*
		 * lexer.print("("); for (Iterator<String> iterator =
		 * params.keySet().iterator(); iterator .hasNext();) { String key =
		 * iterator.next(); String type = params.get(key);
		 * lexer.print(type).print(" ").print(key); if (iterator.hasNext()) {
		 * lexer.print(", "); } } lexer.print(")");
		 */

		/* node descriptor */
		lexer.print(" ");
		tk = lexer.nexttok(SYM_SHARP, SYM_COLON, JAVACC_THROWS);
		if (tk.equals(JAVACC_THROWS)) {
			lexer.print(" ");
			tk = lexer.nexttok(VerifyHub.IDENTIFIER);
			while (true) {
				tk = lexer.nexttok(SYM_COMMA, SYM_COLON, SYM_SHARP);
				if (tk.equals(SYM_COMMA)) {
					tk = lexer.nexttok(VerifyHub.IDENTIFIER);
				} else {
					break;
				}
			}
			lexer.print(" ");
		}
		if (tk.equals(SYM_SHARP)) {
			javaccModel.setTree(true);
			tk = lexer.nexttok(false, VerifyHub.IDENTIFIER);
			lexer.print(tk);
			tk = lexer.nexttok(SYM_LEFT_ROUND_BRACE, SYM_COLON);
			if (tk.equals(SYM_LEFT_ROUND_BRACE)) {
				tk = lexer.skipTo(SYM_LEFT_ROUND_BRACE_CHAR);
				lexer.print(tk);
				tk = lexer.nexttok(SYM_COLON);
			}
		}
		tk = lexer.nexttok(false, SYM_LEFT_BRACE); // should be "{"
		tk = lexer.skip(true, SYM_LEFT_BRACE_CHAR); // skip the Java code
		// initialized
		// part
		methodBodyProduction.setLexer(lexer);
		methodBodyProduction.parse();
		lexer.print("\n");
		lexer.setDump(true);
	}

	/**
	 * @param cache
	 *            the cache to set
	 */
	public void setCache(Map<String, Object> cache) {
		this.cache = cache;
	}

	/**
	 * @param javaccModel
	 *            the javaccModel to set
	 */
	public void setJavaccModel(JavaCCModel javaccModel) {
		this.javaccModel = javaccModel;
	}

	/**
	 * @param methodBody
	 *            the methodBody to set
	 */
	public void setMethodBodyProduction(Production methodBodyProduction) {
		this.methodBodyProduction = methodBodyProduction;
	}

}
